// pages/company-list/company-list.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    AssTypeIdx: '', //行业选择
    areaValue: '', //行业选择
    areaIdx:'',
    area_id:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getCompanyArea(); //搜索行业分类
    this.getUserArea(); //搜索会员区域
    this.getCompList();
  },

  getCompanyArea: function () { //搜索行业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'company/getCompanyArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        wx.setStorageSync('AssocType', res.data.data)
        that.setData({
          AssocType: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  getUserArea: function () { //搜索行业分类
    var that = this;
    wx.request({
      url: app.globalData.host + 'member/getUserArea',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          userarea: res.data.data.userArea
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  bindPickerAssocType: function (e) { //行业选择
    var that = this;
    var AssTypeIdx = e.detail.value;
    var AssocType = that.data.AssocType;
    that.setData({
      AssTypeIdx,
      area_id: AssocType[AssTypeIdx].id
    },()=>{
        that.getCompList();
    })
    
  },
  bindPickerUserArea: function (e) { //行业选择
    var that = this;
    var areaIdx = e.detail.value;
    var userarea = that.data.userarea;
    that.setData({
      areaIdx,
      areaValue: userarea[areaIdx]
    }, () => {
      that.getCompList();
    })

  },

  getCompList(e){ //获取公司列表
    var that = this;
    var area_id = that.data.area_id;
    var areaValue = that.data.areaValue;
    wx.request({
      url: app.globalData.host + 'company/getCompanyList',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data:{
        area_id, areaValue
      },
      method: 'GET',
      success: function (res) {
        console.log(res)
        wx.setStorageSync('AssocType', res.data.data)
        that.setData({
          companyList: res.data.data
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },


  openCardTap(e){ //查看名片
    var that = this;
    var cardId = e.currentTarget.dataset.cardid;
    var userId = wx.getStorageSync('userId')
    var url;
    if (userId == cardId){
      url = '/pages/my-card/my-card?id=' + cardId;
    }else{
      url = '/pages/people-card/people-card?id=' + cardId;
    }
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})